#!/bin/bash

if [ -z "$ALICE_PROOF_AAF_DIR" ]; then
  echo "Error : Set \$ALICE_PROOF_AAF_DIR first !!!"
  exit 1
fi
# 
# if [ ! -d $ALICE_PROOF_AAF_PROOF_DIR ]; then
#   echo "Error : Make link $ALICE_PROOF_AAF_DIR\root which will point to CURRENT ROOT !!!"
#   exit 2
# fi
# 
# if [ ! -e $ALICE_PROOF_AAF_PROOF_DIR/shared/aaf.cf ]; then
#   echo "Error : Config file 'aaf.cf' was not found in $ALICE_PROOF_AAF_PROOF_DIR/shared/ !!!"
#   exit 3
# fi

if [ ! -e $ALICE_PROOF_AAF_DIR/aaf.cf ]; then
  echo "Error : Config file 'aaf.cf' was not found in $ALICE_PROOF_AAF_DIR/ !!!"
  exit 2
fi

source $ALICE_PROOF_AAF_DIR/aaf.cf

function CerateAllVariablesToSubsitute() {
  export ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_DIR"
  ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_PROOF_DIR $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_USER $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_PROOF_XrdSecGSISRVNAME $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_PROOF_MASTER $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_XROOTD_MONALISA $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_XROOTD_SE_NAME $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_XROOTD_REDIRECTOR $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_ALIEN_DIR $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_XROOTD_XRDNAMESPACE $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_PROOF_XPROOFD_DIR $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_PROOF_PORT $ALICE_PROOF_AAF_PROOF_VARIABLES"

  ALICE_PROOF_AAF_PROOF_VARIABLES="AAF_PROOF_MEMORY_LIMIT_VIRT_MB $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="AAF_PROOF_MEMORY_LIMIT_RES_MB $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="AAF_PROOF_IDLE_TIMEOUT_SEC $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="AAF_PROOF_PROOF_MAX_USER_ONLINE $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="AAF_AFDSMGR_MAX_TRANSFERS $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="AAF_AFDSMGR_MAX_CORRUPT_AFTER_FAILS $ALICE_PROOF_AAF_PROOF_VARIABLES"
  ALICE_PROOF_AAF_PROOF_VARIABLES="ALICE_PROOF_AAF_DS_OPT $ALICE_PROOF_AAF_PROOF_VARIABLES"

  export ALICE_PROOF_AAF_PROOF_VARIABLES_PERL=""
  for MYVAR in $ALICE_PROOF_AAF_PROOF_VARIABLES; do
#     echo $MYVAR
    ALICE_PROOF_AAF_PROOF_VARIABLES_PERL=";s/$MYVAR/\$ENV{$MYVAR}/g$ALICE_PROOF_AAF_PROOF_VARIABLES_PERL"
  done
  ALICE_PROOF_AAF_PROOF_VARIABLES_PERL="$ALICE_PROOF_AAF_PROOF_VARIABLES_PERL;"
#   echo $ALICE_PROOF_AAF_PROOF_VARIABLES_PERL
}


CerateAllVariablesToSubsitute

if [ -z "$1" ];then
  prefix=$ALICE_PROOF_AAF_PROOF_XPROOFD_DIR
else
  prefix=$1
fi
cd $prefix
for name in `find . -type f | grep ".tmp" | grep -v '.svn'`; do
  newname=`echo $name | sed s/\.tmp// `;
#   echo "Generating  $newname from $name ..."
  cp -f $name $newname;

  # Substitute all the variables into the templates
  perl -pi -e "$ALICE_PROOF_AAF_PROOF_VARIABLES_PERL" ${prefix}/$newname;
done;


if [ -n "$AAF_PROOF_EXTRA_CONF" ];then
  if [ -f "$AAF_PROOF_EXTRA_CONF" ];then
    echo "Appending $AAF_PROOF_EXTRA_CONF to $ALICE_PROOF_AAF_PROOF_XPROOFD_DIR/prf-main.cf ..."
    cat $AAF_PROOF_EXTRA_CONF >> $ALICE_PROOF_AAF_PROOF_XPROOFD_DIR/prf-main.cf
  fi
fi

unset ALICE_PROOF_AAF_PROOF_VARIABLES
unset ALICE_PROOF_AAF_PROOF_VARIABLES_PERL
# start 

# /etc/init.d/xproofd $1
